package com.linln.modules.system.service.impl;

import com.linln.common.data.PageSort;
import com.linln.modules.system.domain.RequirementDetailsEntity;
import com.linln.modules.system.domain.RequirementImageEntity;
import com.linln.modules.system.repository.RequirementImageRepository;
import com.linln.modules.system.repository.RequirementManageRepository;
import com.linln.modules.system.service.RequirementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.xml.ws.ServiceMode;
import java.util.List;

@Service
public class RequirementImpl implements RequirementService {

    @Autowired
    private RequirementManageRepository requirementManageRepository;

    @Autowired
    private RequirementImageRepository requirementImageRepository;

    @Override
    public RequirementDetailsEntity getRequireByID(Integer Id) {
        return requirementManageRepository.findAllById(Id);
    }

    @Override
    public Page<RequirementDetailsEntity> getRequireAllList(Example<RequirementDetailsEntity> example) {
        PageRequest page = PageSort.pageRequest();
        return requirementManageRepository.findAll(example,page);
    }

    @Override
    public List<RequirementDetailsEntity> getRequireAllByRStat(String RequirementStat) {
        return requirementManageRepository.findAllByRequirementStat(RequirementStat);
    }

    public List<RequirementImageEntity> getRequirementImageByRequireid(Integer Id){
        return requirementImageRepository.findAllByRequireid(Id);
    }
    @Override
    public RequirementDetailsEntity saveRequirementInfo(RequirementDetailsEntity requirementDetailsEntity) {
        return requirementManageRepository.save(requirementDetailsEntity);
    }

    public RequirementImageEntity saveRequirementImage(RequirementImageEntity requirementImageEntity) {
        return requirementImageRepository.save(requirementImageEntity);
    }

    public void deleteRequirementImage(RequirementImageEntity requirementImageEntity){
        requirementImageRepository.delete(requirementImageEntity);
    }

    @Override
    public long getRequirementCountByStat(String stat) {
        if(stat.equals("0")){
            return requirementManageRepository.count();
        }else{
            List<RequirementDetailsEntity> requirementDetailsEntityList = requirementManageRepository.findAllByRequirementStat(stat);
            if(requirementDetailsEntityList != null){
                return requirementDetailsEntityList.size();
            }
            return 0;
        }
    }

}
